package edu.kit.aifb.ldstream.repository.nquad;


import org.openrdf.model.Statement;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.rio.RDFHandler;
import org.openrdf.rio.RDFHandlerException;

public class NQuadHandler implements RDFHandler {

	
	// Connection to write the statements to the repository.
	private Repository rep;
	private RepositoryConnection con;
	
	public NQuadHandler(Repository rep){
		this.rep = rep;
	}
	
	
	@Override
	public void endRDF() throws RDFHandlerException {
		// no special end is needed.
		try {
			this.con.close();
		} catch (RepositoryException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void handleComment(String arg0) throws RDFHandlerException {
		// no comment handling is needed

	}

	@Override
	public void handleNamespace(String arg0, String arg1)
			throws RDFHandlerException {
		// no namespace in n quads
	}

	@Override
	public void handleStatement(Statement stat) throws RDFHandlerException {
		if(stat != null){
			try {
				// adding the statement with the specified context.
				this.con.add(stat);
			} catch (RepositoryException e) {
				e.printStackTrace();
			}

		}
	}

	@Override
	public void startRDF() throws RDFHandlerException {
		// no special start is needed.
		try {
			this.con = this.rep.getConnection();
		} catch (RepositoryException e) {
			e.printStackTrace();
		}
	}

}
